package com.agent.platform.config;

import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Contact;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import io.swagger.v3.oas.models.servers.Server;
import org.springdoc.core.models.GroupedOpenApi;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;

/**
 * SpringDoc OpenAPI 配置
 * 提供Swagger UI文档界面
 */
@Configuration
public class OpenApiConfig {

    @Value("${server.port.business:3000}")
    private int businessPort;

    @Value("${server.port.admin:3001}")
    private int adminPort;

    @Value("${springdoc.swagger-ui.enabled:true}")
    private boolean swaggerEnabled;

    /**
     * 全局OpenAPI配置
     */
    @Bean
    public OpenAPI customOpenAPI() {
        return new OpenAPI()
                .info(new Info()
                        .title("Spring AI Agent Platform API")
                        .version("1.0.0")
                        .description("生产级多租户LLM Agent平台API文档\n\n" +
                                "## 架构说明\n" +
                                "- **业务API端口**: " + businessPort + " - Agent会话管理和运营接口\n" +
                                "- **管理API端口**: " + adminPort + " - Agent CRUD和系统管理接口\n\n" +
                                "## 核心功能\n" +
                                "- 多租户Agent隔离\n" +
                                "- 持续运行的会话管理\n" +
                                "- 完整的监控和成本追踪\n" +
                                "- Spring AI集成（OpenAI、Anthropic）")
                        .contact(new Contact()
                                .name("Agent Platform Team")
                                .email("support@example.com"))
                        .license(new License()
                                .name("MIT License")
                                .url("https://opensource.org/licenses/MIT")))
                .servers(List.of(
                        new Server()
                                .url("http://localhost:" + businessPort)
                                .description("业务API服务器"),
                        new Server()
                                .url("http://localhost:" + adminPort)
                                .description("管理API服务器")
                ));
    }

    /**
     * 业务API分组
     */
    @Bean
    public GroupedOpenApi businessApi() {
        return GroupedOpenApi.builder()
                .group("1-业务API")
                .pathsToMatch("/api/**")
                .build();
    }

    /**
     * 管理API分组
     */
    @Bean
    public GroupedOpenApi adminApi() {
        return GroupedOpenApi.builder()
                .group("2-管理API")
                .pathsToMatch("/admin/**")
                .build();
    }

    /**
     * 监控API分组
     */
    @Bean
    public GroupedOpenApi actuatorApi() {
        return GroupedOpenApi.builder()
                .group("3-监控API")
                .pathsToMatch("/actuator/**")
                .build();
    }

    /**
     * 全部API分组
     */
    @Bean
    public GroupedOpenApi allApi() {
        return GroupedOpenApi.builder()
                .group("0-全部API")
                .pathsToMatch("/**")
                .build();
    }
}
